package com.xyc.merchant.goodsOO.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xyc.global.bean.GoodsBean;
import com.xyc.global.bean.Message;
import com.xyc.global.bean.PageBean;
import com.xyc.merchant.goodsOO.service.GoodsService;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

@RestController
@RequestMapping("goodsOO")
public class GoodsController {

    private Logger logger = Logger.getLogger("GoodsController");
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private GoodsService goodsService;
    @RequestMapping("upload")
    public Message uploadImg(MultipartFile file){
        Message message = new Message();
        try {
            //获取图片输入流
            InputStream inputStream = file.getInputStream();
            //获取文件名
            String filename = file.getOriginalFilename();
            try {

                FTPClient ftp = new FTPClient();
                //连接FTP服务器，默认端口是21
                ftp.connect("192.168.71.142", 21);
                //匿名用户必须使用anonymous登录，密码是邮箱
                boolean login = ftp.login("anonymous", "1756229479@qq.com");
                System.out.println(login);

                //获取ftp服务器的响应
                int replyCode = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(replyCode)) {
                    System.out.println("获取响应失败");
                    message.setCode(1);
                    return message;
                }
                //设置接收文件类型为二进制文件
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                //服务器上创建images文件夹
                ftp.makeDirectory("images");
                //切换ftp默认文件夹
                ftp.changeWorkingDirectory("images");

                String remoteFileName = System.currentTimeMillis() + filename;
                //将本地图片上传到ftp服务器上
                ftp.storeFile(remoteFileName, inputStream);
                //退出登录
                ftp.logout();


                message.setCode(0);

                message.setMsg("http://192.168.71.142:80/images/"+remoteFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }catch (MaxUploadSizeExceededException e){
            message.setCode(1);
            message.setMsg("超过最大上传限制1MB");
        }

        catch (IOException e){
            e.printStackTrace();
        }

        return message;
    }
    @RequestMapping("save")
    public Message saveGoods(@RequestBody GoodsBean goodsBean){
        Message message = new Message();
        try {
            goodsService.saveGoods(goodsBean);
            message.setCode(0);
        } catch (Exception e) {
            message.setCode(1);
            message.setMsg("添加失败");
            e.printStackTrace();
        }
        return message;
    }

    //商户信息
   @HystrixCommand(fallbackMethod="queryGoodsListBeitai")
    @RequestMapping("pageBean")
    public PageBean queryGoodsList(Integer currentPage){
        if (currentPage == null){
            currentPage = 1;
        }
       logger.info("queryGoodsList-->currentPage="+currentPage);
        PageBean pageBean = restTemplate.getForObject("http://goods-server/goods/pageBean?currentPage="+currentPage,PageBean.class);
        //model.addAttribute("pageBean",pageBean);
        return pageBean;
    }

    public PageBean queryGoodsListBeitai(Integer currentPage){
        System.out.println("执行备胎计划");
        if (currentPage == null){
            currentPage = 1;
        }
        PageBean pageBean = restTemplate.getForObject("http://goods-server/goods/pageBeanBeiTai?currentPage="+currentPage,PageBean.class);
        //model.addAttribute("pageBean",pageBean);
        return pageBean;
    }
}
